package com.jh.user.business.impl;

import cn.jh.common.exception.ServiceException;
import com.jh.user.business.CongMingRateBussiness;
import com.jh.user.pojo.CongMingChannel;
import com.jh.user.pojo.CongMingRate;
import com.jh.user.pojo.ShopRegister;
import com.jh.user.repository.CongMingChannelRepository;
import com.jh.user.repository.CongMingRateRepository;
import com.jh.user.repository.ShopRegisterRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class CongMingRateBusinessImpl implements CongMingRateBussiness {

    private static final Logger log = LoggerFactory.getLogger(CongMingRateBusinessImpl.class);

    @Autowired
    private ShopRegisterRepository shopRegisterRepository;
    @Autowired
    private CongMingRateRepository congMingRateRepository;
    @Autowired
    private CongMingChannelRepository congMingChannelRepository;

    @Override
    public List<Map<String, String>> getCongMingRateListByShopId(String shopId) throws ServiceException {
        List<Map<String, String>> list = new ArrayList<>();
        // 1、获取该商户的userId
        ShopRegister byShopId = shopRegisterRepository.findByShopId(shopId);
        if (null == byShopId) {
            throw new ServiceException("为查询到该商户信息！");
        }
        long userId = byShopId.getUserId();

        // 2、根据userId获取该商户个人费率信息
        List<CongMingRate> congMingRateList = congMingRateRepository.findByUserId(userId);
        if (congMingRateList != null && congMingRateList.size() > 0) {
            for (CongMingRate mingRate : congMingRateList) {
                String channelTag = mingRate.getChannelTag();
                BigDecimal rate = mingRate.getRate();

                Map<String, String> map = new HashMap<>();
                map.put("channelTag", channelTag);
                map.put("rate", rate + "");
                map.put("userId", userId + "");
                list.add(map);
            }
        } else {
            // 无个人费率，直接获取通道费率
            List<CongMingChannel> all = congMingChannelRepository.findAll();
            for (CongMingChannel mingChannel : all) {
                String channelTag = mingChannel.getChannelTag();
                BigDecimal rate = mingChannel.getRate();

                Map<String, String> map = new HashMap<>();
                map.put("channelTag", channelTag);
                map.put("rate", rate + "");
                map.put("userId", userId + "");
                list.add(map);
            }
        }
        return list;
    }
}
